//
//  HJLunchAdvertManger.m
//  DaiChaoApp
//
//  Created by apple on 2019/3/7.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "HJLunchAdvertManger.h"
#import "XHLaunchAd.h"
#import "NSString+HJDeviceRegister.h"
#import "BPush.h"

@implementation HJLunchAdvertManger

+(HJLunchAdvertManger *)manager {
    static HJLunchAdvertManger * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[HJLunchAdvertManger alloc]init];
    });
    return manager;
}
+(void)load {
    [self manager];
}
-(instancetype)init {
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
//            [self registerDevice];
//            [self setupXHLaunchAd];
        }];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerDevice) name:@"PushNotification" object:nil];
    }
    return self;
}
#pragma mark -- 注册设备
-(void)registerDevice {
    NSString * url = [Api stringByAppendingString:DeviceRegisterApi];
    // 手机品牌
    NSString * deviceBrand = @"ios";
    // 当前手机系统版本号
    NSString* deviceSystemVersion = [[UIDevice currentDevice] systemVersion];
    // 设备类型
    NSString * deviceType = @"iphone";
    NSString * deviceVersion = [CommonTools appVersion];
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"deviceBrand"] =deviceBrand;
    param[@"deviceSystemVersion"] =deviceSystemVersion;
    param[@"deviceType"] =deviceType;
    param[@"deviceVersion"] = deviceVersion;
    param[@"deviceBaiduChannelId"] = [BPush getChannelId];
    param[@"deviceBaiduUserId"] = [BPush getUserId];
//    NSDictionary * params = @{@"deviceBrand":deviceBrand,
//                              @"deviceSystemVersion":deviceSystemVersion,
//                              @"deviceType":deviceType,
//                              @"deviceVersion":deviceVersion};
    NSLog(@"%@",param);
    [HJHttpTool post:url params:param success:^(HJResponeds * _Nonnull result) {
        NSLog(@"deviceRegister = %@",result);
    } failure:^(id  _Nonnull result) {

    }];
}

-(NSMutableArray *)models {
    if (!_models) {
        _models =[NSMutableArray array];
    }
    return _models;
}
-(void)setupXHLaunchAd {
    [XHLaunchAd setLaunchSourceType:SourceTypeLaunchScreen];
    [XHLaunchAd setWaitDataDuration:3];//请求数据等待时间
    
    NSString *screenUrl = [Api stringByAppendingString:@""];
    NSDictionary *params = @{};
    
    [HJHttpTool post:screenUrl params:params success:^(HJResponeds *result) {
        NSArray * screenArr =@[];
        if (screenArr.count==0) return;
        LaunchAdvertModel * model = screenArr[0];
        [self.models removeAllObjects];
        [self.models addObjectsFromArray:screenArr];
        self.imageAdConfig.imageNameOrURLString = model.materialUrl;
        self.imageAdConfig.openModel = model.jumpTarget;
        [XHLaunchAd imageAdWithImageAdConfiguration:self.imageAdConfig delegate:self];
    } failure:^(id result) {
    }];
    
}
-(XHLaunchImageAdConfiguration *)imageAdConfig {
    if (!_imageAdConfig) {
        _imageAdConfig = [XHLaunchImageAdConfiguration new];
        _imageAdConfig.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH);
        _imageAdConfig.GIFImageCycleOnce = NO;
        _imageAdConfig.duration = 5;
        _imageAdConfig.imageOption = XHLaunchAdImageDefault;
        _imageAdConfig.contentMode = UIViewContentModeScaleAspectFill;
        _imageAdConfig.showFinishAnimate = ShowFinishAnimateFadein;
        _imageAdConfig.showFinishAnimateTime = showFinishAnimateTimeDefault;
        _imageAdConfig.skipButtonType = SkipTypeTimeText;
        _imageAdConfig.showEnterForeground = YES;//后台返回时,是否显示广告
        _imageAdConfig.customSkipView = [self customSkipView];
    }
    return _imageAdConfig;
}
//跳过按钮
-(UIView *)customSkipView{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.layer.cornerRadius = 15.0;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    CGFloat y = XH_FULLSCREEN ? 54 : 30;
    button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-100,y, 85, 30);
    [button addTarget:self action:@selector(skipAction) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
-(void)skipAction{
    [XHLaunchAd removeAndAnimated:YES];
}
- (void)xhLaunchAd:(XHLaunchAd *)launchAd customSkipView:(UIView *)customSkipView duration:(NSInteger)duration {
    UIButton *button = (UIButton *)customSkipView;
    [button setTitle:[NSString stringWithFormat:@"跳过%lds",duration] forState:UIControlStateNormal];
}
#pragma mark - XHLaunchAd delegate - 其他
/**
 广告点击事件回调
 */
-(void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenModel:(id)openModel clickPoint:(CGPoint)clickPoint{
    if(openModel==nil) return;
    LaunchAdvertModel * model = self.models[0];
    HJWebViewController *VC = [[HJWebViewController alloc] init];
    NSString *urlString = (NSString *)openModel;
    VC.URLString = urlString;
    VC.navTitle = model.adTitle;
    [[UIViewController currentViewController].navigationController pushViewController:VC animated:YES];
    
}

@end

@implementation LaunchAdvertModel



@end
